Skip to main content

Cron Plugin

Cron jobs are a standard method of scheduling tasks to run on your server. Cron is a service running in the background that will execute commands (jobs) at a specified time, or at a regular interval. Jobs and their schedules are defined in a configuration file called a crontab.

How to Add

  • Create a godspeed project from the CLI and add the plugin using following command:
  godspeed plugin add @godspeedsystems/plugins-cron-as-eventsource
  • You will find two files added in your project related to the plugin at src/eventsources/types/cron.ts - the type file and src/eventsources/cron.yaml - the instance file.

Type File

eventsources/types/cron.ts

You generally will not need to touch this file, unless you want to extend or customize the cron functionality.

import { EventSource } from '@godspeedsystems/plugins-cron-as-eventsource';
export default EventSource;

This is the file generated by the plugin add command. In case you want to customise the Cron plugin to add new features, you can modify this type file in your project. Checkout the section How to create custom event source

Instance file

src/eventsources/cron.yaml

You can create as many Cron instances in your project as cron1, cron2 and so on. The instance files keep configurations of each instance for the given type of plugin (in following example it is of type: cron)

type: cron

How to Use

1. Define a cron event ( src/events/every_minute_task.yaml )

# event to schedule a task for every minute.

cron.* * * * *.Asia/Kolkata: #this eventkey prefix should be the `type` mentioned in the config `yaml` file, here cron
fn: every_minute

Know more about cron expressions https://crontab.cronhub.io/

2. Cron workflow to schedule ( src/functions/every_minute.ts )

import { GSCloudEvent, GSContext, PlainObject } from "@godspeedsystems/core";

/**
* Prints a message every minute via cron
*/
export default function (ctx: GSContext, args: PlainObject) {
const { logger, childLogger } = ctx;

// Log using both loggers
logger.info("HELLO from CRON");
childLogger.info("HELLO from CRON");

// Return the data
return new GSStatus(true, 200, "message", "HELLO from CRON", undefined);
}

- Plugin Repository
- Issue Tracker
- npm package